package com.ruoyi.project.sell.customer.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.sell.customer.domain.SellCustomer;
import com.ruoyi.project.sell.customer.service.CustomerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("sell/customer")
@Controller
public class ConnContorller extends BaseController {
    @Autowired
    private CustomerService customerServiceImpl;
    //声明公共资源前缀
    private String prefix = "sell/customer";
    //声明公共资源
    @GetMapping
    @RequiresPermissions("sell:customer:view")
    public String sellConn(){
        return prefix+"/sellConn";
    }
    /**
     * 加载客户信息
     */
    @RequiresPermissions("sell:customer:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SellCustomer sellCustomer){
        startPage();    //分页
        List<SellCustomer> orderList = customerServiceImpl.selectCustomerService(sellCustomer);
        System.out.println("客户新"+orderList);
        //getDataTable(orderList)响应请求分页数据
        return getDataTable(orderList);
    }
    //声明添加的公共资源
    @GetMapping("/add")
    public String sellAdd(){
        return prefix+"/add";
    }
    /**
     * 校验客户信息
     */
    @PostMapping("/checkCusNameUnique")
    @ResponseBody
    public String checkCusNameUnique(SellCustomer sellCustomer)
    {
        return customerServiceImpl.checkCusNameUnique(sellCustomer);
    }
    /**
     * 新增客户信息
     */
    @RequiresPermissions("sell:customer:add")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated SellCustomer sellCustomer)
    {
        if (UserConstants.Customer_NAME_NOT_UNIQUE.equals(customerServiceImpl.checkCusNameUnique(sellCustomer)))
        {
            return error("新增客户'" + sellCustomer.getCusName() + "'失败，客户名称已存在");
        }
        return toAjax(customerServiceImpl.insertSellCustomer(sellCustomer));
    }
    /**
     * 删除客户信息
     */
    @RequiresPermissions("sell:customer:remove")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(customerServiceImpl.deleteSellCustomerByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
    /**
     * 修改客户信息，跳转修改页面并完成数据的回显
     */
    @GetMapping("/edit/{cusId}")
    public String edit(@PathVariable("cusId") Integer cusId, ModelMap mmap)
    {
        mmap.put("customer", customerServiceImpl.selectCustomerById(cusId));
        return prefix + "/edit";
    }
    /**
     * 修改保存公告类别
     */
    @RequiresPermissions("sell:customer:edit")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated SellCustomer sellCustomer)
    {
        if (UserConstants.Customer_NAME_NOT_UNIQUE.equals(customerServiceImpl.checkCusNameUnique(sellCustomer)))
        {
            return error("修改客户信息'" + sellCustomer.getCusName() + "'失败，客户名称已存在");
        }
        return toAjax(customerServiceImpl.updateCustomerService(sellCustomer));
    }

}
